﻿/*
NOTE:
*/
package com.cf.core.Utils{
	import flash.events.Event;
	import flash.events.ProgressEvent;
	import flash.events.EventDispatcher;
	import flash.events.HTTPStatusEvent;
	import flash.events.IEventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.navigateToURL;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.net.URLVariables;
	import com.cf.core.events.LoadEvent;
	import com.cf.core.Utils.tools; // debug
	public class cacheURL extends EventDispatcher {
		private var __Loader:URLLoader;
		private var __Request:URLRequest;
		private var __Vars:URLVariables;
		private var __method:String;
		private var __BasePath:String;
		private var __pathOption:String;
		public function cacheURL() { RELEASE(); }//fn
		
		public function RELEASE():void{
			__Request = new URLRequest();
			__Vars = new URLVariables();
			__BasePath="";
			__pathOption="";
		}//fn
		
		public function server(_Uri:String,_method:String="GET"):void{
			__BasePath = _Uri;
			switch(_method){
				default: case "get": case "GET": case URLRequestMethod.GET:
					__method = URLRequestMethod.GET;
				break;
				case "post": case "POST": case URLRequestMethod.POST:
					__method = URLRequestMethod.POST;
				break;
			}//case
		}//fn
		
		public function addParam(_varname:String, _str:String):void{
			vars[_varname] = _str;
			trace("Added Param["+_varname+"] = "+ vars[_varname]);
		}//fn
		
		private function getReady():void{
			__Loader = new URLLoader();
			configureListeners(__Loader);
			__Request=new URLRequest(__BasePath+__pathOption);
			__Request.method = __method;
			__Request.data = vars;
			trace("cacheURL : sendto:"+__BasePath+__pathOption+"\nMethod :"+__Request.method+"\nVar :"+ vars );
		}//fn
		public function sentByForm():void{
			getReady();
			__Loader.load(__Request);
		}//fn
		public function sentByURL(_win:String="_self"):void{
			getReady();
			navigateToURL(__Request,_win);
		}//fn
		
		

		
		////////////////////////
		//
		// Getter Setter
		//
		////////////////////////
		public function set vars(_v:URLVariables):void{__Vars=_v}
		public function get vars():URLVariables{return __Vars}
		public function set path(_s:String):void{__pathOption=_s}
		public function get path():String{return __pathOption}

		//
		//private AREA==========================================================
		//
		private function configureListeners(dispatcher:IEventDispatcher):void{
			dispatcher.addEventListener(Event.COMPLETE, handleDataSent);
            dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, handleSecurityError);
            dispatcher.addEventListener(IOErrorEvent.IO_ERROR, handleIOError);
		}
		
		private function deConfigureListeners(dispatcher:IEventDispatcher):void{
			dispatcher.removeEventListener(Event.COMPLETE, handleDataSent);
			dispatcher.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, handleSecurityError);
            dispatcher.removeEventListener(IOErrorEvent.IO_ERROR, handleIOError);
		}
		
		protected function handleIOError(e:IOErrorEvent):void{
			trace("handleIOError");
			deConfigureListeners(IEventDispatcher(e.target));
			dispatchEvent(new LoadEvent(LoadEvent.FAIL, {msg: "Error: handleDataSentError retry..."}));
		}//fn
        protected function handleSecurityError(e:SecurityErrorEvent):void{
			trace("handleSecurityError");
			deConfigureListeners(IEventDispatcher(e.target));
			dispatchEvent(new LoadEvent(LoadEvent.FAIL, {msg: "Error: handleSecurityError retry..."}));
        }//fn
		protected function handleDataSent(e:Event):void{
			trace("handleDataSent "+e);
			deConfigureListeners(IEventDispatcher(e.target));
			dispatchEvent(new LoadEvent(LoadEvent.ONLOAD));
		}//fn
	}//class
}//package